import Phaser from 'phaser';
import config from './config';

window.zooming = false;
window.zoomAmount = 0;
window.boom = null;
var size = new Phaser.Rectangle(0,0,800,600);
window.statistical={
    score:0,
    star:0
};
var time = (new Date).getTime();
function update() {
    // console.log("游戏更新");
    let time2 = (new Date).getTime();
    let fps = parseInt(1000/(time2- time)) 
    // console.log();
    time = time2;
    // 鼠标点击移动
    shubiaozhuizong.call(this);
    // boom 爆炸抖动
    boom.call(this);
    // 碰撞检查
    // window.stargroup
    
    if(this.physics.arcade.overlap(window.player, window.stargroup, collideCallback, processCallback,this)){
        console.log("collide");
    }
    // 通过方向键移动
    shangxiazuoyou.call(this)
    
        // 缩放处理
        zoom.call(this);
        // 相机追踪
        follow.call(this);
    
    
}

/**
 * 碰撞开始
 */
function collideCallback(player,star){
    // console.log('collideCallback',player,star);
    star.kill();
    window.statistical.star++;
    window.statistical.score++;

    // 创建新的星星
    let star19 = window.stargroup.create(this.world.randomX, this.world.randomY, 'star');
    this.physics.arcade.enable(star19,false);


    window.scorecard[1].setText("计分: "+ window.statistical.score)

}

/**
 * 碰撞移除
 */
function processCallback(){
    return true;
}

/**
 * 缩放缩小处理
 */
function zoom() {
    if (window.zooming) {
        let newbili =  this.camera.scale.x + window.zoomAmount;
        // this.camera.scale.x += window.zoomAmount;
        // this.camera.scale.y += window.zoomAmount;
        if(config.world.width * newbili < config.view.width ){
            console.log("最小了");
    
        }else{
            this.camera.scale.x = this.camera.scale.y = newbili;
        }
        console.log('zoom',this.camera.scale.x);


        // console.log(this.camera.bounds);
        // console.log(this.input.activePointer.worldX, this.input.activePointer.worldY);

        // this.camera.focusOnXY(this.input.activePointer.worldX, this.input.activePointer.worldY);

        // this.world.position.x = -this.input.activePointer.worldX * this.camera.scale.x;
        // this.world.position.y = -this.input.activePointer.worldY * this.camera.scale.y;

        // this.world.pivot.x = this.input.activePointer.worldX * this.camera.scale.x;
        // this.world.pivot.y = this.input.activePointer.worldY * this.camera.scale.y;

    }

}

function shangxiazuoyou(){
    return ;
    let pos;
    let cursors = this.input.keyboard.createCursorKeys();
    if (cursors.up.isDown) {
        pos =[
            window.player.x,window.player.y-1
        ];
        this.physics.arcade.moveToXY(window.player, pos[0] ,pos[1] ,get_speed());
    } else if (cursors.down.isDown) {
        pos =[
            window.player.x,window.player.y+1
        ];
        this.physics.arcade.moveToXY(window.player, pos[0] ,pos[1] ,get_speed());
    }else if (cursors.left.isDown) {
        pos =[
            window.player.x-1,window.player.y
        ];
        this.physics.arcade.moveToXY(window.player, pos[0] ,pos[1] ,get_speed());
    } else if (cursors.right.isDown) {
        pos =[
            window.player.x+1,window.player.y
        ];
        this.physics.arcade.moveToXY(window.player, pos[0] ,pos[1] ,get_speed());
    }
}
function get_speed(){
    if(window.player.zom >= 9.9){
        return 20;
    }
    return config.player.init_speed * (1-window.player.zom/10);
}

/**
 * 鼠标追踪移动
 */
function shubiaozhuizong(){
        // 点击
     // 鼠标按下时动
     if (this.input.activePointer.leftButton.isDown) {
         // 左键点击
        // 400表示速度
        // 已经到达鼠标了，就不动了
        let pos = jisuanxy.call(this,this.input.x, this.input.y);
        this.physics.arcade.moveToXY(window.player, pos[0] ,pos[1] ,get_speed());
        // console.log([window.player.position.x,window.player.position.y],[this.input.x, this.input.y] ,pos,get_speed());
        // if (Phaser.Rectangle.contains(window.player,pos[0] ,pos[1] )) {
        //    window.player.body.velocity.setTo(0, 0);
        // }
    } else {
        window.player.body.velocity.setTo(0, 0);
    }
}

/**
 * 计算点击位置位于世界的什么位置
 * @param int x 
 * @param int y 
 */
function jisuanxy(x,y){
    let scale  = this.camera.scale.x;
    // console.log('scale',this.camera.scale.x)
    let nx = (x / scale) + (this.camera.x/ scale);
    let ny =  (y /scale) + (this.camera.y/scale) ;
    return [nx,ny]
}

//爆炸 效果
function boom(){
    
    // if(randomNum(1,3) ==2 ){
        // 20% 的帧 
        if(window.boom === null){
            //没有爆炸
             // 摄像机追随玩家 
            // this.camera.follow(window.player);
        }else{
            // this.camera.unfollow(window.player);
            // console.log(randomNum(1,10));
            // this.camera.x +=10;
            this.camera.x = window.boom.x + randomNum(1,20);
            this.camera.y = window.boom.y + randomNum(1,20);;
        }
    // }
    
}

function follow(){
    let jishu = 15;
    let pianyix = (jishu * window.player.scale.x  )-jishu
    let x = parseInt( (window.player.position.x * this.camera.scale.x) - (config.view.width/2) + pianyix );
    let y =parseInt( (window.player.position.y * this.camera.scale.y) - (config.view.height/2) + pianyix ) ;
    if(x < 0 ){
        x = 0;
    }
    if(y < 0 ){
        y = 0;
    }

    // console.log('follow ',x,y);
    this.camera.x =x;
    this.camera.y = y;
    // this.camera.setPosition(x, y);
}
/**
 * 随机数生成
 * @param {*} minNum 
 * @param {*} maxNum 
 */
function randomNum(minNum, maxNum) {
    switch (arguments.length) {
      case 1:
        return parseInt(Math.random() * minNum + 1, 10);
        break;
      case 2:
        return parseInt(Math.random() * ( maxNum - minNum + 1 ) + minNum, 10);
        //或者 Math.floor(Math.random()*( maxNum - minNum + 1 ) + minNum );
        break;
      default:
        return 0;
        break;
    }
  } 
export default update;